package com.flight.trans.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import com.flight.trans.R;
import com.flight.trans.bean.LoginBean.LoginData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.EditTextWatcher;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ToastUtils;

import java.util.HashMap;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtUserName ,// 用户名
            mEtPassword ;//密码
    private ImageButton mIbClearUser ,//清除
            mIbDisplayPassword;//显示密码

    @Override
    protected int setContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        mEtUserName = findViewById(R.id.mEtUserName);
        mEtPassword = findViewById(R.id.mEtPassword);
        mIbClearUser = findViewById(R.id.mIbClearUser);
        mIbDisplayPassword = findViewById(R.id.mIbDisplayPassword);
    }

    @Override
    protected void initData() {
        mEtUserName.addTextChangedListener(new OnEditTextWatcher(mIbClearUser));
        mEtPassword.addTextChangedListener(new OnEditTextWatcher(mIbDisplayPassword));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mIbClearUser:
                if(CommonUtils.isFastDoubleClick())
                    return;
                // 清除
                mEtUserName.setText("");
                mEtPassword.setText("");
                break;
            case R.id.mIbDisplayPassword:
                if(CommonUtils.isFastDoubleClick())
                    return;
                // 显示隐藏/密码
                if(mEtPassword.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()){
                    // 隐藏
                    mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIbDisplayPassword.setImageDrawable(getDrawable(R.drawable.selector_hide_button));
                }else {
                    // 隐藏
                    mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIbDisplayPassword.setImageDrawable(getDrawable(R.drawable.selector_display_login));
                }
                mEtPassword.setSelection(mEtPassword.getText().toString().length());
                break;
            case R.id.mBtnLogin:
                // 登录
                clickLogin();
                break;
        }
    }

    /**
     * 登录
     */
    private void clickLogin(){
        String userName = mEtUserName.getText().toString();
        String password = mEtPassword.getText().toString();

        if(CommonUtils.isTestLoginClick() && TextUtils.isEmpty(userName) && TextUtils.isEmpty(password)){
            userName = "jya";
            password = "123123123";
            ToastUtils.showToast("测试账号，请勿使用。");
        }

        if(TextUtils.isEmpty(userName)){
            ToastUtils.showToast(R.string.toast_user_empty);
        }else if(TextUtils.isEmpty(password)){
            ToastUtils.showToast(R.string.toast_password_empty);
        }else {
            valueMap = new HashMap<>();
            valueMap.put(Constant.USER_NAME , userName);
            valueMap.put(Constant.PASSWORD , password);

            String finalUserName = userName;
            NetworkUtils.login(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    LoginData loginData = (LoginData) body;
                    ConstantInfo.TOKEN = loginData.getAccess_token();
                    ConstantInfo.USER_NAME = finalUserName;
                    ConstantInfo.EXPIRES_IN = loginData.getExpires_in();

                    turnToAct(MainActivity.class);
                }
            }, valueMap);
        }
    }

    /**
     * 控制显示隐藏
     */
    private class OnEditTextWatcher extends EditTextWatcher {
        private ImageButton imageButton ;
        public OnEditTextWatcher(ImageButton imageButton){
            this.imageButton = imageButton ;
        }
        @Override
        public void afterTextChanged(Editable s) {
            imageButton.setVisibility(TextUtils.isEmpty(s.toString()) ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public void onBackPressed() {
        onBackDoubleClick();
    }
}
